package com.youoor.mvc;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.youoor.common.utils.CommonUtils;
import com.youoor.domain.CardFavo;
import com.youoor.domain.CardLink;
import com.youoor.domain.CardMsg;
import com.youoor.domain.UserCard;
import com.youoor.domain.UserInfo;
import com.youoor.dto.CardInfo;
import com.youoor.dto.RetDTO;
import com.youoor.interceptors.RequestAttribute;
import com.youoor.service.CardLinkService;
import com.youoor.service.CardMsgService;
import com.youoor.service.UserCardService;

@Controller
@RequestMapping(value = { "/open", "/core", "/inner" }, produces = "application/json")
public class MsgController extends AbstractController{

	private static final Logger logger = LoggerFactory.getLogger(MsgController.class);

	@Autowired
	private CardMsgService cardMsgService;
	
	@Autowired
	private UserCardService userCardService;
	
	@Autowired
	private CardLinkService cardLinkService;

	@RequestMapping("/hi")
	public RetDTO hi(@RequestAttribute("cardId") String cardId) {
		/*UserCard userCardm = getUserCard();
		if (userCardm.getCardId().intValue() == Integer.valueOf(cardId)) {
			
		}*/
		Date nowTime = new Date();
		UserCard userCard = getUserCard();
		CardMsg cardMsg = new CardMsg();
		cardMsg.setCardId(Integer.valueOf(cardId));
		cardMsg.setMainCardId(userCard.getCardId());
		cardMsg.setCtime(nowTime);
		cardMsg.setUtime(nowTime);
		cardMsg.setId(idWorker.nextId());
		cardMsg.setOptType(0);
		cardMsg.setContent(CommonUtils.getshuoshuo());
		cardMsgService.save(cardMsg);
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
		
	}
	
	@RequestMapping("/getMyHi")
	public RetDTO getMyHi(
			@RequestAttribute("pageIndex") String pageIndex,
			@RequestAttribute("num") String num) {
		UserCard userCard = getUserCard();
		List<CardMsg> cardMsgList = cardMsgService.getCardMsgListByMainCardId(userCard.getCardId(),Integer.valueOf(pageIndex),Integer.valueOf(num));
		List<CardInfo>  hiList = new ArrayList<CardInfo>();
		if(!CollectionUtils.isEmpty(cardMsgList)){
			for (CardMsg cardMsg:cardMsgList) {
				UserCard userCard1 = userCardService.selectByPrimaryKey(cardMsg.getMainCardId());
				CardInfo cardInfo = new CardInfo();
				List<CardLink> links = cardLinkService.getLinksByCardId(cardMsg.getMainCardId());
				cardInfo.setUserCard(userCard1);
				cardInfo.setLinks(links);
				cardInfo.setContent(cardMsg.getContent());
				cardInfo.setHiCtime(CommonUtils.DateToStr(cardMsg.getCtime()));
				hiList.add(cardInfo);
			}
		}
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("hiList",hiList);
		retDTO.markSuccess();
		return retDTO;
		
	}
	
	@RequestMapping("/checkNewMsg")
	public RetDTO checkNewMsg(){
		UserInfo userInfo = getCurrUser();
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("flag",false);
		retDTO.markSuccess();
		return retDTO;
	}
}
